<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>模块</title>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <style>
        img{max-width: 650px;}
    </style>
</head>
<body class="container">
<h1>模块</h1>
<h3>通过 exports 对象把 world 作为模块的访问接口</h3>
<div class="well">
    <h4>创建一个hello模块，即新建一个hello.js文件，内容如下：</h4>
    <xmp>
        exports.word=function () {
            console.log('Hello Word');
        }
    </xmp>
    <h4>下面是引用hello模块的示例</h4>
    <xmp>
        var hello=require('./hello');
        hello.word();
    </xmp>
</div>
<h3>把一个对象封装到模块中</h3>
<div class="well">
    <h4>创建一个HelloWord模块，即新建一个HelloWord.js文件，内容如下：</h4>
    <xmp>
        function HelloWord() {
            var name;
            this.setName=function (name_) {
                name=name_;
            };
            this.sayHello = function() {
                console.log('Hello ' + name);
            };
        };
        //把一个对象封装到模块中
        module.exports = HelloWord;
    </xmp>
    <h4>下面是引用HelloWord模块的示例</h4>
    <xmp>
        var hello=require('./hello');
        hello.word();
        var HelloWord=require('./HelloWord');
        var hword=new HelloWord();
        hword.setName('张三');
        hword.sayHello();
    </xmp>
</div>
</body>
</html>